package com.six.order.service.impl;

import com.alipay.api.domain.AlipayBossBaseProcessSignVerifyModel;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.six.core.entity.Goods;
import com.six.core.entity.Goodscart;
import com.six.core.entity.Order;
import com.six.core.vo.Result;
import com.six.order.dao.GoodscartMapper;
import com.six.order.feign.GoodsFeign;
import com.six.order.service.GoodsCartService;
import com.six.order.service.OrderService;
import com.six.order.dao.OrderMapper;
import com.six.order.vo.DeliverGoodsVo;
import com.six.order.vo.OrderShopVo;
import com.six.order.vo.OrderUserVo;
import com.six.order.vo.dto.UserOrderVo;
import com.six.order.vo.dto.shopOrderVo;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.concurrent.ForkJoinPool;

/**
* @author 汪鹏
* @description 针对表【sto_order】的数据库操作Service实现
* @createDate 2023-03-04 09:10:31
*/
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService{

    @Autowired
    private OrderMapper orderDao;
    @Autowired
    private GoodscartMapper goodscartMapper;
    @Autowired
    private GoodsFeign goodsFeign;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private GoodsCartService goodsCartService;

    public static void main(String[] args) {

        UUID uuid = UUID.randomUUID();
        System.out.println(uuid);
        String s = RandomStringUtils.randomAlphanumeric(15);
        System.out.println("s = " + s);
    }

    @Override
    public Result createOrder(List<Order> orderList) {
        String orderId = RandomStringUtils.randomAlphanumeric(15);
        for (Order order : orderList) {
            order.setOrderid(orderId);
        }
        //批量添加订单信息
        boolean b = this.saveBatch(orderList);
        if (b){
            //订单信息添加成功后，删除购物车中对应的商品信息
            goodsCartService.deleteInfo(orderList);
            //订单信息添加成功后，推送消息到普通交换机
            rabbitTemplate.convertAndSend("pt_exchange","",orderId);
            return new Result(200,"下单成功",orderId);
        }
        return new Result(500,"下单失败");
    }


    /** 用户查询自己的订单 */
    @Override
    public Result userSelect(int currentPage, int pageSize,OrderUserVo orderUserVo) {

        List<UserOrderVo> orders = orderDao.userSelect(orderUserVo);
        PageHelper.startPage(currentPage,pageSize);
        List<UserOrderVo> userOrderVos1 = orderDao.userSelect2(orderUserVo);

        for (UserOrderVo userOrderVo : userOrderVos1) {
            double sum=0;
            List<UserOrderVo> userOrderVos = new ArrayList<>();
            for (UserOrderVo order : orders) {
                if (userOrderVo.getOrderid().equals(order.getOrderid())){
                    userOrderVos.add(order);
                    sum+=order.getPay();
                }
            }
            userOrderVo.setTotalprice(sum);
            userOrderVo.setList(userOrderVos);
        }
        PageInfo<UserOrderVo> pageInfo = new PageInfo<>(userOrderVos1);
        return new Result(200,"查询成功",pageInfo);
    }

    /** 根据id查询goodsid  根据查询goodsid查询成交量*/
    @Override
    public Result selectPaystateByGoodsid(Integer id) {
        Goods goods = goodsFeign.selectGoodsByid(id);
        Integer orders = orderDao.selectPaystateByGoodsid(goods.getGoodsid());
        if (orders != null){
            return new Result(2000,"查询成功",orders);
        }
        return new Result(5000,"查询失败");
    }


    /**
     * 根据orderid查询多条数据,判断支付状态
     */
    @Override
    public void selectOrderStatus(String orderId) {
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq("orderid",orderId);
        List<Order> orders = orderDao.selectList(wrapper);
        Order order = orders.get(0);
        if (order.getPaystate()!=0){
            this.quXiaoOrder(orderId);
        }
    }

    /** 店铺查询本店铺的销售订单信息，通过自己的店铺id查询 */
    @Override
    public Result shopSelect(int currentPage, int pageSize, OrderShopVo orderShopVo) {
        List<shopOrderVo> shopOrderVos = orderDao.shopSelect(orderShopVo);
        PageHelper.startPage(currentPage,pageSize);
        List<shopOrderVo> shopOrderVos1 = orderDao.shopSelect2(orderShopVo);

        for (shopOrderVo shopOrderVo : shopOrderVos1) {
            double sum = 0;
            List<shopOrderVo> list = new ArrayList<>();
            for (shopOrderVo orderVo : shopOrderVos) {
                if (shopOrderVo.getOrderid().equals(orderVo.getOrderid())){
                    orderVo.setPrice(orderVo.getPay()/orderVo.getGoodsnum());
                    list.add(orderVo);
                    sum= orderVo.getTotalprice();
                }
            }
            shopOrderVo.setTotalprice(sum);
            shopOrderVo.setList(list);
        }
        PageInfo<shopOrderVo> shopOrderVoPageInfo = new PageInfo<>(shopOrderVos1);
        return new Result(200,"查询成功",shopOrderVoPageInfo);
    }

    /** 店铺发货，通过修改订单中的sendstate状态，修改为0 */
    @Override
    public Result deliverGoods(DeliverGoodsVo deliverGoodsVo) {
        UpdateWrapper<Order> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("sendstate",0)
                .eq("orderid",deliverGoodsVo.getOrderid())
                .eq("shopid",deliverGoodsVo.getShopid());
        int i = orderDao.update(new Order(),updateWrapper);
        return new Result(200,"发货成功");
    }

    /**  用户收货，修改该条订单中的sendstate状态，修改为2 */
    @Override
    public Result receiving(DeliverGoodsVo deliverGoods) {
        UpdateWrapper<Order> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("sendstate",2)
                .eq("orderid",deliverGoods.getOrderid())
                .eq("goodsid",deliverGoods.getGoodsid());
        int i = orderDao.update(new Order(), updateWrapper);
        return new Result(200,"收货成功");
    }

    /** 用户评价，评价过后将订单中的状态改为已评价，这样一个用户只能评价一次 用户添加评论时远程调用*/
    @Override
    public Integer addComment(String orderid, String goodsid) {
        UpdateWrapper<Order> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("remarkstate",0)
                .eq("order",orderid)
                .eq("goodsid",goodsid);
        return orderDao.update(new Order(), updateWrapper);
    }

    /**  用户订单删除订单 */
    @Override
    public Result deleteOrder(String orderid) {
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("orderid",orderid);
        int i = orderDao.delete(queryWrapper);
        return new Result(200,"删除成功",null);
    }

    /** 用户取消订单*/
    @Override
    public Result quXiaoOrder(String orderId) {
        UpdateWrapper<Order> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("is_deleted",1)
                .set("paystate",2)
                .eq("orderid",orderId);
        int i = orderDao.update(new Order(), updateWrapper);
        return new Result(200,"取消成功");
    }


}




